package com.hongyi.web.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hongyi.web.dto.SysPostDto;
import com.hongyi.web.mapper.SysPostMapper;
import com.hongyi.web.mapper.SysUserPostMapper;
import com.hongyi.web.pojo.SysPost;
import com.hongyi.web.query.SysPostQueryDto;
import com.hongyi.web.query.SysUserQueryDto;
import com.hongyi.web.service.ISysPostService;
import com.hongyi.web.utils.PageResult;
import com.hongyi.web.vo.PostVo;
import com.hongyi.web.vo.SysPostVo;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * 岗位信息 服务层处理
 *
 * @author ruoyi
 */
@Service
public class SysPostServiceImpl implements ISysPostService
{
    @Autowired
    private SysPostMapper postMapper;

    @Autowired
    private SysUserPostMapper userPostMapper;

    /**
     * 查询所有岗位
     *
     * @return 岗位列表
     */
    @Override
    public List<SysPost> selectPostAll()
    {
        List<SysPost> posts = postMapper.selectPostAll();
        return posts;
    }

    @Override
    public List<Long> selectPostListByUserId(Long userId) {
        return postMapper.selectPostListByUserId(userId);
    }

    @Override
    public void deletePostByIds(Long[] postIds) {
        postMapper.deletePostByIds(postIds);
    }

    @Override
    public void updatePost(SysPostDto sysPostDto) {
        SysPost sysPost = new SysPost();
        BeanUtils.copyProperties(sysPostDto,sysPost);
        postMapper.updatePost(sysPost);
    }

    @Override
    public void insertPost(SysPostDto sysPostDto) {
        SysPost sysPost = new SysPost();
        BeanUtils.copyProperties(sysPostDto,sysPost);
        postMapper.insertPost(sysPost);
    }

    @Override
    public PostVo selectPostById(Long postId) {
        SysPost posts = postMapper.selectPostById(postId);
        PostVo sysPostVo = new PostVo();
        BeanUtils.copyProperties(posts,sysPostVo);
        return sysPostVo;
    }

    @Override
    public PageResult<PostVo> selectPage(SysPostQueryDto sysPostQueryDto, Integer pageNum, Integer pageSize) {

        // 设置分页参数
        PageHelper.startPage(pageNum, pageSize);

        List<SysPost> sysPosts = postMapper.selectPostList(sysPostQueryDto);

        List<PostVo> sysPostVos = new ArrayList<>();

        for (SysPost sysPost : sysPosts) {
            PostVo sysPostVo = new PostVo();
//            sysPostVo.setPostId(sysPost.getPostId());
//            sysPostVo.setPostCode(sysPost.getPostCode());
//            sysPostVo.setPostName(sysPost.getPostName());
//            sysPostVo.setPostSort(sysPost.getPostSort());
//            sysPostVo.setStatus(sysPost.getStatus());
//            sysPostVo.setCreateTime(sysPost.getCreateTime());
            BeanUtils.copyProperties(sysPost,sysPostVo);
            sysPostVos.add(sysPostVo);
        }
        // 返回结果
        PageInfo<PostVo> voPageInfo = new PageInfo<>(sysPostVos);
        return new PageResult<>(voPageInfo.getTotal(), voPageInfo.getList());
    }

}
